package com.yijumao.controller;

import com.yijumao.entity.User;
import com.yijumao.entity.UserFile;
import com.yijumao.service.UserFileService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author konley
 * @date 2020-10-26 21:04
 */
@Controller
@RequestMapping("/file")
public class FileController {

    @Autowired
    private UserFileService userFileService;


    @GetMapping("/showAll")
    public String findAll(Model model, HttpSession session){
        User user = (User) session.getAttribute("user");
        if(user!=null){
            List<UserFile> files = userFileService.findByUserId(user.getId());
            model.addAttribute("files",files);
            return "showAll";
        }else{
            return null;
        }
    }

    @PostMapping("/upload")
    public String upload(MultipartFile uploadFile, HttpSession session) throws IOException {
        //获取用户id
        User user = (User)session.getAttribute("user");
        Integer userId = user.getId();
        //获取文件的原始名称
        String oldFileName = uploadFile.getOriginalFilename();
        //获取文件后缀
        String extension = "."+FilenameUtils.getExtension(oldFileName);
        //生成新文件名
        String newFileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis()))+
                UUID.randomUUID().toString().replace("-","")+
                extension;
        //文件大小
        long size = uploadFile.getSize();
        //文件类型
        String type = uploadFile.getContentType();
        //根据日期动态生成目录
        String realPath = ResourceUtils.getURL("classpath:").getPath()+ "/static/files";
        //  -上传目录
        String dateFormat = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));
        String dateDirPath = realPath + "/" + dateFormat;
        //  -不存在，创建目录，mkdirs创建多级目录
        if(!new File(dateDirPath).exists()){
            new File(dateDirPath).mkdirs();
        }
        //处理文件上传
        uploadFile.transferTo(new File(dateDirPath,newFileName));
        //将文件信息放入数据库中，是否图片、下载次数、上传日期由service层完成
        UserFile userFile = new UserFile
                (null,oldFileName,newFileName,extension,
                        "/files/"+dateFormat,size+"",type,
                        null,null,null,userId);
        userFileService.save(userFile);
        return "redirect:/file/showAll";
    }
}
